﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xfs;

namespace XfsServer
{
    [XfsObjectSystem]
    public class XfsServerTestAwakeSystem : XfsAwakeSystem<XfsServerTest>
    {
        public override void Awake(XfsServerTest self)
        {
            TestOneActorEntity testOne = XfsComponentFactory.CreateWithId<TestOneActorEntity>(20211023);
            TestOneActorEntity testOne1 = XfsComponentFactory.CreateWithId<TestOneActorEntity>(20211024);
            TestOneActorEntity testOne2 = XfsComponentFactory.CreateWithId<TestOneActorEntity>(20211025);
            TestOneActorEntity testOne3 = XfsComponentFactory.CreateWithId<TestOneActorEntity>(20211026);

            self.testoneId = testOne.InstanceId;
            self.testone1Id = testOne1.InstanceId;
            self.testone2Id = testOne2.InstanceId;
            self.testone3Id = testOne3.InstanceId;
            testOne.mesage = "我是测试体1号";
            testOne1.mesage = "我是测试体2号";
            testOne2.mesage = "我是测试体3号";
            testOne3.mesage = "我是测试体4号";

        }
    }
   
    [XfsObjectSystem]
    public class XfsServerTestUpdateSystem : XfsUpdateSystem<XfsServerTest>
    {
        public override void Update(XfsServerTest self)
        {
            //this.Test2SessionSend(self);
            //this.Test4Send(self);            

        }

        bool issend = false;
        async void Test4Send(XfsServerTest self)
        {
            //if (!issend) return;
            time += 1;
            if (time > restime)
            {

                time = 0;
                issend = true;

                XfsSession session;

                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

                if (sessions.Count > 0)
                {
                    foreach (XfsSession ses in sessions.Values)
                    {
                        session = ses;

                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " 88. XfsServerTestSystem: " + session.GetComponent<XfsAsyncUserToken>().Socket.LocalEndPoint);
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " 89. XfsServerTestSystem: " + self.testoneId);


                        C4S_Ping resqustC = new C4S_Ping();
                        resqustC.Opcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>().GetOpcode(resqustC.GetType());
                        resqustC.Message = self.testoneId.ToString();

                        S4C_Ping? responseC = await session.Call(resqustC) as S4C_Ping;

                        ///从服务端发回来的信息
                        if (responseC != null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 161. 收到客户端回复: " + responseC.Message);
                        }
                        else
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 165." + ": responseC is null");
                        }
                    }


                }
            }
        }
        

        int time = 0;
        int restime = 4000;
        void Test0SessionSend(XfsServerTest self)
        {
            time += 1;
            if (time > restime)
            {
                time = 0;

                XfsSession session;

                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

                if (sessions.Count > 0)
                {
                    session = sessions.Values.ToList()[0];


                    C4S_Heart resqustC = new C4S_Heart();
                    resqustC.Opcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>().GetOpcode(resqustC.GetType());
                    resqustC.Message = XfsTimeHelper.Now().ToString();

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 132. 开始打电话给服务器." + session.RemoteAddress);
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 133. 消息体长度." + resqustC.ToString().Length);

                    session.Send(resqustC);




                }


            }
        }
        async void Test2SessionSend(XfsServerTest self)
        {
            time += 1;
            if (time > restime)
            {
                time = 0;
                XfsOpcodeTypeComponent xfsOpcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>();
                XfsMessageDispatcherComponent xfsMessage = XfsGame.XfsSence.GetComponent<XfsMessageDispatcherComponent>();

             
                XfsSession session;

                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

                if (sessions.Count > 0)
                {
                    session = sessions.Values.ToList()[0];

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " 48. XfsServerTestSystem: " + session.GetComponent<XfsAsyncUserToken>().Socket.LocalEndPoint);
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " 49. XfsServerTestSystem: " + self.call);


                    C4S_Ping resqustC = new C4S_Ping();
                    resqustC.Opcode = XfsGame.XfsSence.GetComponent<XfsOpcodeTypeComponent>().GetOpcode(resqustC.GetType());
                    resqustC.Message = self.call;

                    if (session.RemoteAddress != null)
                    {
                        resqustC.Message = self.call + " " + session.RemoteAddress.ToString();
                    }

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 54. 我是服务器，开始打电话给客户端. " + session.RemoteAddress);

                    S4C_Ping? responseC = await session.Call(resqustC) as S4C_Ping;

                    ///从服务端发回来的信息
                    if (responseC != null)
                    {
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 61. 我是服务器，收到客户端回复的信息: " + responseC.Message);
                    }
                    else
                    {
                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 65." + ": responseC is null");
                    }

                }
            }
        }
    }


    public class XfsServerTest : XfsEntity
    {
        public string longin = "2020-10-18";
        public string? par { get; set; }
        public long testoneId { get; set; }
        public long testone1Id { get; set; }
        public long testone2Id { get; set; }
        public long testone3Id { get; set; }

        public bool IsUserLogin = false;

        public float time = 0;

        public float restime = 3000;

        public string call = "起点服户端，2021.12.21.";

    }

}
